import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import Layout from '@/module-dashboard/pages/layout'
import { getToken } from '@/utils/auth'
import net from '@/constants/constants'
// 定义
const _import = require('./import_' + process.env.NODE_ENV) // 懒加载 导包

const whiteList = ['/login', '/authredirect'] // 白名单 无需跳转

// 配置
Vue.use(Router)
NProgress.configure({ showSpinner: false }) // NProgress Configuration
Vue.prototype.NET = net
/**
 * 基础路由
 *
* hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
* alwaysShow: true               if set true, will always show the root menu, whatever its child routes length
*                                if not set alwaysShow, only more than one route under the children
*                                it will becomes nested mode, otherwise not show the root menu
* redirect: noredirect           if `redirect:noredirect` will no redirct in the breadcrumb
* name:'router-name'             the name is used by <keep-alive> (must set!!!)
* meta : {
    roles: ['admin','editor']     will control the page roles (you can set multiple roles)
    title: 'title'               the name show in submenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar,
    noCache: true                if true ,the page will no be cached(default is false)
  }
**/
export const constantRouterMap = [
  {
    path: '/login',
    component: _import('dashboard/pages/login'),
    hidden: true
  },
  {
    path: '/authredirect',
    component: _import('dashboard/pages/authredirect'),
    hidden: true
  },
  {
    path: '/404',
    component: _import('dashboard/pages/404'),
    hidden: true
  },
  {
    path: '/401',
    component: _import('dashboard/pages/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/courses/list',
    hidden: true
  },
  {
    path: '/courses',
    component: Layout,
    redirect: 'noredirect',
    name: 'courses',
    meta: {
      title: '课程',
      icon: 'icon_kecheng_nor'
    },
    children: [
      {
        path: 'list',
        component: _import('xczx/pages/courses'),
        name: 'courses-list',
        meta: { title: '课程管理', noCache: true }
      },
      {
        path: 'media',
        component: _import('xczx/pages/media'),
        name: 'media',
        meta: { title: '媒资管理', noCache: true }
      },
      {
        path: 'category',
        component: _import('xczx/pages/category'),
        name: 'category',
        meta: { title: '分类管理', noCache: true }
      },
      {
        path: 'evaluation',
        component: _import('xczx/pages/evaluation'),
        name: 'evaluation',
        meta: { title: '评价管理', noCache: true }
      }
    ]
  },
  {
    path: '/institutions',
    component: Layout,
    redirect: 'noredirect',
    name: 'institutions',
    meta: {
      title: '机构',
      icon: 'icon_jigou_nor'
    },
    children: [
      {
        path: 'list',
        component: _import('xczx/pages/institutions'),
        name: 'list',
        meta: { title: '机构列表', noCache: true }
      },
      {
        path: 'insdetail',
        component: _import('xczx/pages/insdetail'),
        hidden: true,
        name: 'insdetail',
        meta: { title: '详细资料', noCache: true }
      },
      {
        path: 'insaudit',
        component: _import('xczx/pages/insaudit'),
        name: 'insaudit',
        meta: { title: '机构审核', noCache: true }
      },
      {
        path: 'auditdetail',
        component: _import('xczx/pages/auditdetail'),
        hidden: true,
        name: 'auditdetail',
        meta: { title: '机构审核-详细资料', noCache: true }
      }
    ]
  }
  // {
  //   path: '/users',
  //   component: Layout,
  //   redirect: 'noredirect',
  //   name: 'users',
  //   meta: {
  //     title: '用户',
  //     icon: 'icon_yonghu_nor'
  //   },
  //   children: [
  //     {
  //       path: 'list',
  //       component: _import('xczx/pages/users'),
  //       name: 'list',
  //       meta: { title: '用户列表', noCache: true }
  //     }
  //   ]
  // },
  // {
  //   path: '/orders',
  //   component: Layout,
  //   redirect: 'noredirect',
  //   name: 'orders',
  //   meta: {
  //     title: '订单',
  //     icon: 'icon_dingdan_nor'
  //   },
  //   children: [
  //     {
  //       path: 'list',
  //       component: _import('xczx/pages/orders'),
  //       name: 'list',
  //       meta: { title: '订单列表', noCache: true }
  //     }
  //   ]
  // }
]

/**
 * 配置路由
 **/
let router = new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})
router.beforeEach((to, from, next) => {
  NProgress.start()
  let token = process.env.VUE_APP_SERVER_AUTHORIZATION
  if (token) {
    next()
  } else {
    if (getToken()) {
      /* has token */
      if (to.path === '/login') {
        next({ path: '/' })
        NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
      } else {
        next()
      }
    } else {
      /* has no token */
      if (whiteList.indexOf(to.path) !== -1) {
        // 在免登录白名单，直接进入
        next()
      } else {
        next('/login') // 否则全部重定向到登录页
        NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
      }
    }
  }

})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})

/**
 * 导出 基础路由
 **/
export default router

/**
 * 导出 业务路由
 **/
export let asyncRouterMap = [{ path: '*', redirect: '/404', hidden: true }]
